﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RedDragon.Common.Dtos;
using RedDragon.ERP.API.Application.Commands.Supplieries;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Controllers
{
    /// <summary>
    /// 供应商控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SuppliersController : ControllerBase
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public SuppliersController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("CreateSupplier")]
        public async Task<Result> CreateSupplier(CreateSupplierCommand cmd)
        {
            //创建时间默认当前时间
            cmd.CreatedTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取所有国家信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchCountry")]
        public async Task<Result> SearchCountry([FromQuery] SearchCountryCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取城市信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchCity")]
        public async Task<Result> SearchCity([FromQuery] SearchCityCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取供应商分类信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchClassifications")]
        public async Task<Result> SearchClassifications([FromQuery] SearchClassificationCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取供应商信息列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("SearchSupplier")]
        public async Task<Result<PageResultDto<SupplierDto>>> SearchSupplier(SearchSupplierCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 删除供应商列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpDelete("DeleteSupplier")]
        public async Task<Result> DeleteSupplier(DeleteSupplierCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 根据供应商ID获取供应商信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("DetailsSupplier")]
        public async Task<Result> DetailsSupplier([FromQuery] DetailsSupplierCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 修改供应商信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPut("UpdateSupplier")]
        public async Task<Result> UpdateSupplier(UpdateSupplierCommand cmd)
        {
            cmd.UpdateTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 导出供应商信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("ExportSupplier")]
        public async Task<IActionResult> ExportSupplier([FromQuery] ExportSupplierCommand cmd)
        {
            var res = await _mediator.Send(cmd);
            return File(res.FileBytes, res.MimeType, res.FileName);
        }

        /// <summary>
        /// 导入供应商信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("ImportSupplier")]
        public async Task<Result> ImportSupplier(ImportSupplierCommand cmd)
         {
            return await _mediator.Send(cmd);
        }

    }
}
